package com.thingsgrid.device.service.impl;

import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.Topic;
import com.thingsgrid.device.service.ITopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TopicServiceImpl implements ITopicService {
	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 新增
	 * @param topic
	 */
	@Override
	public void saveTopic(Topic topic) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(new Date());
		topic.setCreateTime(format);
		mongoTemplate.save(topic);
	}

	/**
	 * 删除
	 * @param topicId
	 */
	@Override
	public void removeTopic(String topicId) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("_id").is(topicId));
		mongoTemplate.remove(query,Topic.class);
	}

	/**
	 * 修改
	 * @param topic
	 */
	@Override
	public void updateTopic(Topic topic) {
		Query query = new Query();
		query.addCriteria(Criteria.where("topicClassName").is(topic.getTopicClassName()));
		Update update = new Update();
		update.set("topicClassName", topic.getTopicClassName());
		update.set("topicClass", topic.getTopicClass());
		update.set("topicType", topic.getTopicType());
		update.set("topicDescribe", topic.getTopicDescribe());
		update.set("create_author",topic.getCreateAuthor());
		update.set("createTime",topic.getCreateTime());
		mongoTemplate.updateFirst(query, update, Topic.class);
	}

	/**
	 * 查找所有
	 * @return
	 */
	@Override
	public List<Topic> findAll() {
		return mongoTemplate.findAll(Topic.class);
	}

	/**
	 * 查找所有systemtopic
	 * @return
	 */
	@Override
	public List<Topic> findSystemTopic() {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("topicType").is("system")); //fxy
		return(mongoTemplate.find(query,Topic.class));
	}
	/**
	 * 查找所有Usertopic
	 * @return
	 */
	@Override
	public List<Topic> findUserDefineTopic(){
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("topicType").is("user")); //fxy
		return(mongoTemplate.find(query,Topic.class));
	}

	/**
	 * 根据name查
	 * @param topicClassName
	 * @return
	 */
	@Override
	public Topic findTopicByName(String topicClassName) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("topicClassName").is(topicClassName));
		Topic one = mongoTemplate.findOne(query,Topic.class);
		return one;
	}

	/**
	 * 根据id查
	 * @param topicId
	 * @return
	 */
	@Override
	public Topic findTopicById(String topicId) {
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("topicId").is(topicId));
		Topic one = mongoTemplate.findOne(query,Topic.class);
		return one;
	}


	/**
	 * 说明：分页查询
	 * @author 陈学栋
	 * @since 2019/10/14 16:38
	 */
	@Override
	public HashMap<String, Object> findAll(Integer current, Integer size, String productId) {
		Query query = new Query();
		query.addCriteria(Criteria.where("productId").is(productId));
		HashMap<String, Object> map = new HashMap<>();
		//根据productId查询一条Product
		Product product = mongoTemplate.findOne(query, Product.class, "product");
		//查询一条Product中的topictopicClassList
		List<Topic> topicClass = product.getTopicClass();
		//如果产品已经有添加进去的TopicClass，要做去重，在分页的时候就要多查询topicClass.size()条数据。
		List<Topic> topicList = mongoTemplate.find(new Query().skip((current - 1) * size).limit(size +topicClass.size()), Topic.class);
		//int topicAddedCount = 0;
		for(Topic topic:topicClass){
			String topicClassName = topic.getTopicClassName();
			Topic topicByName = this.findTopicByName(topicClassName);
			topicList.remove(topicByName);
		}
		//
		int count = this.findAll().size() - topicClass.size();
		map.put("TopicList",topicList);
		map.put("TopicCount",count);
		return map;
	}
}
